package com.xywy.bloodandroid.adapter;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.bean.Remind;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 发现 - 提醒
 */
public class RemindAdapter extends BaseAdapter {

	private Context mContext;
	private List<Remind> list;
	private Calendar calendar;
	private DecimalFormat df;
	private SharedPreferences sp;
//	private Drawable drawable;
	private Handler handler;

	public RemindAdapter(Context context, List<Remind> list, Handler handler) {
		this.mContext = context;
		calendar = Calendar.getInstance();
		df = new DecimalFormat("00");
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		this.list = list;
		this.handler = handler;
	}

	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Hold hold;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.remind_item, null);

			hold = new Hold();

			hold.hourmin = (TextView) convertView.findViewById(R.id.tv_times);
			hold.yaoname = (TextView) convertView
					.findViewById(R.id.tv_medicine_name);
			hold.jiliang = (TextView) convertView.findViewById(R.id.tv_unit);

			convertView.setTag(hold);
		} else {
			hold = (Hold) convertView.getTag();
		}
		final Remind remind = list.get(position);

		calendar.setTimeInMillis(remind.getTime());
		hold.hourmin.setText((calendar.get(Calendar.HOUR_OF_DAY)) + ":"
				+ df.format(calendar.get(Calendar.MINUTE)) + "");
		hold.yaoname.setText(remind.getYaoname());
		hold.jiliang.setText(remind.getDosage());

//		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
//				drawable.getMinimumHeight());

		convertView.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Message msg = Message.obtain();
				msg.what = 0x1101;
				msg.arg1 = position;
				msg.obj = v;
				handler.sendMessage(msg);
				return false;
			}
		});
		return convertView;
	}

	public class Hold {
		// RelativeLayout rlYmjl;
		TextView hourmin; // 时间
		TextView yaoname; // 药品名称
		TextView jiliang; // 药品剂量
	}

	class remindListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			int p = (Integer) view.getTag();
			list.get(p);
			System.out.println(p);
			notifyDataSetChanged();
		}
	}

	public List<Remind> getList() {
		return list;
	}
}
